﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class frmDonHangSapGiao : Form
    {
        public frmDonHangSapGiao()
        {
            InitializeComponent();
        }

        private void frmDonHangSapGiao_Load(object sender, EventArgs e)
        {
            DonHangSapGiaoBUS bus = new DonHangSapGiaoBUS();
            List<DonHangSapGiaoDTO> ds = bus.LayDS();
            int stt = 1;
            dgvDonHangSapGiao.Rows.Clear();
            for (int i = 0; i < ds.Count; i++)
            {
                int ngay = ds[i].ThoiGianConLai / 1440;
                int temp = ds[i].ThoiGianConLai % 1440;
                int gio = temp / 60;
                temp = temp % 60;

                string tgconlai = ngay + " Ngày " + gio + " Giờ " + temp + " Phút ";
                dgvDonHangSapGiao.Rows.Add(stt, ds[i].MaHoaDon, ds[i].TenNguoiNhan, ds[i].SDTNguoiNhan, ds[i].DiaChi, ds[i].NgayGiao, tgconlai);
                stt++;
            }

            if (ds.Count > 0)
            {
                int rowSelected = dgvDonHangSapGiao.CurrentRow.Index;
                int ma = (int)dgvDonHangSapGiao.Rows[rowSelected].Cells[1].Value;
                //List<ChiTietHoaDonDTO> dsct = bus.LayChiTietDHTheoMaDH(ma);
                List<ChiTietDonHangSapGiaoDTO> dsct = bus.LayChiTietDHTheoMaDH(ma);
                double tongtien = 0;
                stt = 1;
                dgvDSChiTiet.Rows.Clear();
                for (int i = 0; i < dsct.Count; i++)
                {
                    //dgvDSChiTiet.Rows.Add(stt, dsct[i].MaHoaDon, dsct[i].MaMatHang, dsct[i].MaXuatXu, dsct[i].MaDonViTinh, dsct[i].SoLuong, dsct[i].DonGia + " VNĐ", dsct[i].SoLuong * dsct[i].DonGia + " VNĐ");
                    dgvDSChiTiet.Rows.Add(stt, dsct[i].MaHoaDon, dsct[i].TenMatHang, dsct[i].XuatXu, dsct[i].DonViTinh, dsct[i].SoLuong, dsct[i].DonGia + " VNĐ", dsct[i].TongTien + " VNĐ");
                    stt++;
                    tongtien = tongtien + dsct[i].TongTien;
                }
                txtTongTien.Text = tongtien.ToString() + " VNĐ";
            }
        }

        private void dgvDonHangSapGiao_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowSelected = dgvDonHangSapGiao.CurrentRow.Index;
            int ma = (int)dgvDonHangSapGiao.Rows[rowSelected].Cells[1].Value;
            DonHangSapGiaoBUS bus = new DonHangSapGiaoBUS();
            //List<ChiTietHoaDonDTO> ds = bus.LayChiTietDHTheoMaDH(ma);
            List<ChiTietDonHangSapGiaoDTO> ds = bus.LayChiTietDHTheoMaDH(ma);
            int stt = 1;
            double tongtien = 0;
            dgvDSChiTiet.Rows.Clear();
            for (int i = 0; i < ds.Count; i++)
            {
                //dgvDSChiTiet.Rows.Add(stt, ds[i].MaHoaDon, ds[i].MaMatHang, ds[i].MaXuatXu, ds[i].MaDonViTinh, ds[i].SoLuong, ds[i].DonGia + " VNĐ", ds[i].SoLuong*ds[i].DonGia + " VNĐ");
                dgvDSChiTiet.Rows.Add(stt, ds[i].MaHoaDon, ds[i].TenMatHang, ds[i].XuatXu, ds[i].DonViTinh, ds[i].SoLuong, ds[i].DonGia + " VNĐ", ds[i].TongTien + " VNĐ");
                stt++;
                tongtien = tongtien + ds[i].TongTien;
            }
            txtTongTien.Text = tongtien.ToString() + " VNĐ";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
